
$(document).ready(function(){
    laydate.render({
      elem: '#startTime'
      ,type: 'datetime'
    });

    laydate.render({
      elem: '#endTime'
      ,type: 'datetime'
    });
    selectAddOption("status","proCategoryType","");

    init();
});

function init(){
	validateRule();
//	firstStepInit();
}


function saveFirst() {
//alert(JSON.stringify($('#firstForm').serialize()));
	$.ajax({
		cache : true,
		type : "POST",
		headers:PRIVATE_HEADERS,
		url : API_SERVER_URL + "smm/project/update",
		data : $('#firstForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
//			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.alert("保存成功。");
//				secondStepInit();
			} else {
				parent.layer.alert(data.msg)
			}
		}
	});
}

function saveSecond() {
//alert(JSON.stringify($('#firstForm').serialize()));
	$.ajax({
		cache : true,
		type : "POST",
		headers:PRIVATE_HEADERS,
		url : API_SERVER_URL + "smm/project/update",
		data : $('#secondForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
//			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.alert("保存成功。");
//				secondStepInit();
			} else {
				parent.layer.alert(data.msg)
			}
		}
	});
}

function addSla(){
        var t = $("tr:last").clone().appendTo("table").after().val('');
//        $('.templateMes:last').after(newTr);
//        $('.templateMes:last').find('select[class*=type]').val($('.templateMes:last').prev().find('select[class*=type]').val());

}
//删除一行
function removeLine(obj){
      layer.confirm('确定要删除选中的记录？', {
      		btn : [ '确定', '取消' ]
      	}, function(index) {
      	//alert($(obj).parents("tr").html());
      	   var trObj=$(obj).parents("tr");
      	   var trParent=$(trObj).parents("tbody");
            $(trObj).remove();
            var indexTr=1;
            var str="";

            $(trParent).children("tr").each(function(){
                $(this).children().first().html('SLA'+indexTr);
               indexTr++;
            });
            layer.close(index);
      	})
}
//计算和
function myParseInt(num){
//console.log(num)
     if(num==''){
         num=0;
       }else{
          num=parseInt(num);
       }

       return num;
}
function addTime(obj){
   var  aTime=$(obj).val();
   var aName=$(obj).attr("name");
//   console.log(aName);
   aName=aName.substring(0,aName.lastIndexOf("["));

   var one=myParseInt($("[name='"+aName+"[0].kpiValue"+"']").val());

   var two=myParseInt($("[name='"+aName+"[1].kpiValue"+"']").val());

   var three=myParseInt($("[name='"+aName+"[2].kpiValue"+"']").val());

   var chu=one+two+three;

   $("[name='"+aName+"[3].kpiValue"+"']").val(parseInt(one)+parseInt(two)+parseInt(three));
   //console.log($("[name='"+aName+"']").val());
   //console.log(aName);
}
/**
 * 初始化可校验的表单，第一步和第二步的表单
 * @returns
 */
function validateRule() {
	$("#firstForm").validate({
		submitHandler:function(form){
            saveFirst();
        }
	});
	$("#secondForm").validate({
		submitHandler:function(form){
            saveSecond();
        }
	});
}

